package com.flight.trans.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.activity.LeavePortWeighingDetailsActivity;
import com.flight.trans.adapter.LeavePorReturnAdapter;
import com.flight.trans.adapter.LeavePorWeighingAdapter;
import com.flight.trans.bean.LeavePortWeightingBean.LeavePortWeightingData;
import com.flight.trans.global.Constant;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.util.ChinaToPinyin;
import com.flight.trans.util.ProgressUtils;
import com.flight.trans.view.HeaderTitleView;
import com.flight.trans.base.BaseFragment;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 出库称重
 */
public class LeavePortWeighingFragment extends BaseFragment {

    private HeaderTitleView mHeaderView ;

    private TwinklingRefreshLayout mRefreshLayout ;
    private RecyclerView mRvContainer ;
    private RelativeLayout mRlContentNull ;

    private TextView mTvContainerNo,//容器编号
            mTvFlightNumber ,//航班号
            mTvFlightDate ,//航班日期
            mTvOrderCount ,//运单数量
            mTvOrderNumber ,//运单件数
            mTvOrderWeight ,//货邮重量
            mTvContainerWeight ,//容器重量
            mTvTotalWeight ,//总重量
            mTvWeightingWeight ,//称重重量
            mTvWeighingPerson ;//称重人

    private LeavePorWeighingAdapter mWeightingAdapter;
    private List<LeavePortWeightingData> mWeightingDataList,mSearchDataList;

    private int mSortState = -1;

    @Override
    public int setContentView() {
        return R.layout.fragment_leave_port_weighing;
    }

    @Override
    public void initView() {
        mHeaderView = mView.findViewById(R.id.mHeaderView);
        mRefreshLayout = mView.findViewById(R.id.mRefreshLayout);
        mRvContainer = mView.findViewById(R.id.mRvContainer);
        mRlContentNull = mView.findViewById(R.id.mRlContentNull);

        mTvContainerNo = mView.findViewById(R.id.mTvContainerNo);//容器编号
        mTvFlightNumber = mView.findViewById(R.id.mTvFlightNumber);//航班号
        mTvFlightDate = mView.findViewById(R.id.mTvFlightDate);//航班日期
        mTvOrderCount = mView.findViewById(R.id.mTvOrderCount);//运单数量
        mTvOrderNumber = mView.findViewById(R.id.mTvOrderNumber);//货邮单号
        mTvOrderWeight = mView.findViewById(R.id.mTvOrderWeight);//货邮重量
        mTvContainerWeight = mView.findViewById(R.id.mTvContainerWeight);//容器重量
        mTvTotalWeight = mView.findViewById(R.id.mTvTotalWeight);//总重量
        mTvWeightingWeight = mView.findViewById(R.id.mTvWeightingWeight);//称重重量
        mTvWeighingPerson = mView.findViewById(R.id.mTvWeighingPerson);//称重人
    }

    @Override
    public void initData() {
        initHeaderView();

        mTvContainerNo.setOnClickListener(this::onClickSort);//容器编号
        mTvFlightNumber.setOnClickListener(this::onClickSort);//航班号
        mTvFlightDate.setOnClickListener(this::onClickSort);//航班日期
        mTvOrderCount.setOnClickListener(this::onClickSort);//运单数量
        mTvOrderNumber.setOnClickListener(this::onClickSort);//货邮单号
        mTvOrderWeight.setOnClickListener(this::onClickSort);//货邮重量
        mTvContainerWeight.setOnClickListener(this::onClickSort);//容器重量
        mTvTotalWeight.setOnClickListener(this::onClickSort);//总重量
        mTvWeighingPerson.setOnClickListener(this::onClickSort);//称重人

        mTvWeighingPerson.setVisibility(mHeaderView.isDone() ? View.VISIBLE : View.GONE);
        mTvWeightingWeight.setVisibility(mHeaderView.isDone() ? View.VISIBLE : View.GONE);

        setRefreshListener(mRefreshLayout);

        ProgressUtils.showProgress(mActivity);
    }

    @Override
    public void initConnect() {
        if(mHeaderView == null) return;

        valueParams = new HttpParams();
        valueParams.put(Constant.Key_Done , mHeaderView.isDone());
        if(mHeaderView.isDone())
            valueParams.put(Constant.Query_Date , mHeaderView.getSelectDate());

        NetworkUtils.getLeavePortWeighting(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mWeightingDataList = (List<LeavePortWeightingData>) body;

                onScanResult(mHeaderView.getSearchContent());
            }
        }, valueParams);
    }

    private void initAdapter(){
        mRlContentNull.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.VISIBLE : View.GONE);
        mRefreshLayout.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.GONE : View.VISIBLE);

        if(mWeightingAdapter == null) {
            mWeightingAdapter = new LeavePorWeighingAdapter(mActivity, mSearchDataList, mHeaderView.isDone());
            mRvContainer.setAdapter(mWeightingAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mWeightingAdapter.setOnItemClickListener(new ItemOnClickListener());
        }else {
            mWeightingAdapter.updateAdapter(mSearchDataList, mHeaderView.isDone());
        }
    }

    /**
     * 单击事件
     */
    private class ItemOnClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            mIntent = new Intent(mActivity , LeavePortWeighingDetailsActivity.class);
            mBundle = new Bundle();
            mBundle.putString(Constant.Container_Code , mSearchDataList.get(position).getContainerCode());
            mBundle.putInt(Constant.Container_Type , mSearchDataList.get(position).getContainerType());
            mBundle.putInt(Constant.ID , mSearchDataList.get(position).getId());
            mBundle.putBoolean(Constant.IsEditor , !mHeaderView.isDone());
            mIntent.putExtras(mBundle);
            turnActForResult(mIntent);
        }
    }

    private void initHeaderView() {
        mHeaderView.addOnHeaderListener(new HeaderTitleView.OnHeaderListener(){
            @Override
            public void onSearch(String search) {
                onScanResult(search);
            }

            @Override
            public void onPartChange(int isChecked) {
                ProgressUtils.showProgress(mActivity);
                mTvWeighingPerson.setVisibility(mHeaderView.isDone() ? View.VISIBLE : View.GONE);
                mTvWeightingWeight.setVisibility(mHeaderView.isDone() ? View.VISIBLE : View.GONE);
                initConnect();
            }

            @Override
            public void onDateChange(String date) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }
        });
    }

    @Override
    public void onScanResult(String code) {
        mHeaderView.setSearchContent(code);

        mSearchDataList = new ArrayList<>();

        if(!TextUtils.isEmpty(code)) {
            for (int i = 0; i < mWeightingDataList.size(); i++)
                if(mWeightingDataList.get(i).getContainerCode() != null && mWeightingDataList.get(i).getContainerCode().contains(code))
                    mSearchDataList.add(mWeightingDataList.get(i));
        }else {
            mSearchDataList.addAll(mWeightingDataList) ;
        }

        if(mSearchDataList != null)
            if(mSortState == 0){
                Collections.reverse(mSearchDataList);
            }else if(mSortState != -1){
                Collections.sort(mSearchDataList, new SortComparator(mSortState));
            }

        initAdapter();
    }

    public void onClickSort(View view){
        mSortState = (mSortState != 0 && R.id.mTvContainerNo == view.getId()) ? 0 : (mSortState == 0 ? -1 : mSortState) ;
        mSortState = (mSortState != 1 && R.id.mTvOrderCount == view.getId()) ? 1 : mSortState == 1 ? -1 : mSortState ;
        mSortState = (mSortState != 2 && R.id.mTvOrderNumber == view.getId()) ? 2 : mSortState == 2 ? -1 : mSortState ;
        mSortState = (mSortState != 3 && R.id.mTvOrderWeight == view.getId()) ? 3 : mSortState == 3 ? -1 : mSortState ;
        mSortState = (mSortState != 4 && R.id.mTvContainerWeight == view.getId()) ? 4 : mSortState == 4 ? -1 : mSortState ;
        mSortState = (mSortState != 5 && R.id.mTvTotalWeight == view.getId()) ? 5 : mSortState == 5 ? -1 : mSortState ;
        mSortState = (mSortState != 6 && R.id.mTvWeighingPerson == view.getId()) ? 6 : mSortState == 6 ? -1 : mSortState ;
        mSortState = (mSortState != 7 && R.id.mTvWeightingWeight == view.getId()) ? 7 : mSortState == 7 ? -1 : mSortState ;

        mTvContainerNo.setTextColor(mActivity.getColor(mSortState == 0 ? R.color.color_red : R.color.color_blue));
        mTvOrderCount.setTextColor(mActivity.getColor(mSortState == 1 ? R.color.color_red : R.color.color_blue));
        mTvOrderNumber.setTextColor(mActivity.getColor(mSortState == 2 ? R.color.color_red : R.color.color_blue));
        mTvOrderWeight.setTextColor(mActivity.getColor(mSortState == 3 ? R.color.color_red : R.color.color_blue));
        mTvContainerWeight.setTextColor(mActivity.getColor(mSortState == 4 ? R.color.color_red : R.color.color_blue));
        mTvTotalWeight.setTextColor(mActivity.getColor(mSortState == 5 ? R.color.color_red : R.color.color_blue));
        mTvWeightingWeight.setTextColor(mActivity.getColor(mSortState == 6 ? R.color.color_red : R.color.color_blue));
        mTvWeighingPerson.setTextColor(mActivity.getColor(mSortState == 7 ? R.color.color_red : R.color.color_blue));

        onScanResult(mHeaderView.getSearchContent());
    }

    /**
     * 排序集合
     */
    private class SortComparator implements Comparator<LeavePortWeightingData> {

        private int type;

        public SortComparator(int type){
            this.type = type;
        }

        @Override
        public int compare(LeavePortWeightingData data1, LeavePortWeightingData data2) {
            switch (type){
                case 0:
                    return ChinaToPinyin.getInstance().sort(data1.getContainerCode() , data2.getContainerCode());
                case 1 :
                    return data2.getOrderNum() - data1.getOrderNum();
                case 2 :
                    return data2.getGoodsNum() - data1.getGoodsNum();
                case 3 :
                    return (int) (data2.getGoodsWeight() - data1.getGoodsWeight());
                case 4 :
                    return (int) (data2.getContainerWeight() - data1.getContainerWeight());
                case 5 :
                    return (int) ((data2.getContainerWeight() + data2.getGoodsNum()) - (data1.getContainerWeight() + data1.getGoodsWeight()));
                case 6 :
                    return (int) (data2.getWeight() - data1.getWeight());
                case 7 :
                    return ChinaToPinyin.getInstance().sort(data1.getWeighingName() , data2.getWeighingName());
                default:
                    return 0 ;
            }
        }
    }
}
